from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
import os
import uvicorn

# 导入路由和中间件
from routers import landmarks, routes, files, auth, admin, pages
from middleware import admin_auth_middleware
from database import engine
from models import Base

# 创建FastAPI应用
app = FastAPI(
    title="校园历史文化数字地图", 
    description="用于管理校园历史文化地标和路径的后端系统"
)

# 创建数据库表
Base.metadata.create_all(bind=engine)

# 挂载静态文件目录
os.makedirs("static", exist_ok=True)
os.makedirs("static/media", exist_ok=True)
app.mount("/static", StaticFiles(directory="static"), name="static")

# 创建.well-known目录以支持Chrome DevTools等工具
os.makedirs(".well-known/appspecific", exist_ok=True)
app.mount("/.well-known", StaticFiles(directory=".well-known"), name="well_known")

# 设置模板目录
templates = Jinja2Templates(directory="templates")

# 添加中间件
app.middleware("http")(admin_auth_middleware)
# 包含路由
app.include_router(pages.router)  # 页面路由和公共API
app.include_router(landmarks.router)
app.include_router(routes.router)
app.include_router(files.router)
app.include_router(auth.router)
app.include_router(admin.router)


# 启动应用
if __name__ == "__main__":
    uvicorn.run("app:app", host="0.0.0.0", port=8080, reload=True)
